import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/dio_utils.dart';
import 'package:material_collection_app/utils/utils.dart';
import 'package:material_collection_app/widget/common/top_bar.dart';

class ManageDetailPage extends StatefulWidget  {
  final Map arguments;

  ManageDetailPage({Key key, this.arguments}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<ManageDetailPage> {
  Map detail;
  bool loadSuccess = false;

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  // 加载数据
  _loadData({bool loadMore = false, bool screen}) async {
    DioUtils.request("goods/details",
      context: context,
      method: "POST",
      params: {
        'id': widget.arguments['id']
      },
      onSuccess: (res) {
        if(res['code'] == 1) {
          if(mounted) {
            setState(() {
              detail = res['data'];
              loadSuccess = true;
            });
          }
        }else {
          Utils.showText(text: res['msg']);
        }
      },
      onError: (error) {}
    );
  }

  Widget _buildCardHead(title) {
    return Container(
      width: 680.w,
      height: 90.w,
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
        border: Border(bottom:BorderSide(width: 1,color: Color(0xFFF1F1F1)))
      ),
      child: Text(title,
        style: TextStyle(
          color: Color(0xFF333333), fontSize: 16, fontWeight: FontWeight.bold
        ),
      ),
    );
  }

  Widget _buildRowText(title, text, [Color color]) {
    return Padding(
      padding: EdgeInsets.fromLTRB(0, 15.w, 0, 15.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(title, style: TextStyle(
            color: Color(0xFF999999),
            fontSize: 14
          )),
          Text(text.toString() == 'null' ? '--' : text.toString(), style: TextStyle(
            color: color ?? Color(0xFF333333),
            fontSize: 14
          )),
        ],
      ),
    );
  }

  Widget _buildRowPicture() {
    List<Widget> images = [];

    for(var item in detail['enter_files']) {
      images.add(
        Container(
          width: 100.w,
          height: 100.w,
          decoration: BoxDecoration(
            color: Utils.hexToColor('#F1F3F4'),
            borderRadius: BorderRadius.circular(4.w),
          ),
          margin: EdgeInsets.only(right: 10.w),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(4.w),
            child: Image.network(DioUtils.API_PREFIX+item['file_path'], 
              fit: BoxFit.cover,
            )
          )
        )
      );
    }

    return Padding(
      padding: EdgeInsets.fromLTRB(0, 15.w, 0, 15.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text('图片', style: TextStyle(
            color: Color(0xFF999999),
            fontSize: 14
          )),
          Container(
            width: 500.w,
            child: Wrap(
              spacing: 10.w,
              runSpacing: 25.w,
              alignment: WrapAlignment.end,
              children: images
            )
          )
        ]
      )
    );
  }

  // Widget _buildRowPicture(Map detail) {
  //   return Padding(
  //     padding: EdgeInsets.fromLTRB(0, 15.w, 0, 15.w),
  //     child: Row(
  //       mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //       children: <Widget>[
  //         Text('图片', style: TextStyle(
  //           color: Color(0xFF999999),
  //           fontSize: 14
  //         )),
  //         Row(
  //           children: <Widget>[
  //             Container(
  //               margin: EdgeInsets.only(right: 10.w),
  //               child: ClipRRect(
  //                 borderRadius: BorderRadius.circular(4.w),
  //                 child: Image.asset('assets/pro.png', 
  //                   width: 100.w,
  //                   height: 100.w,
  //                 ),
  //               ),
  //             ),
  //             Container(
  //               margin: EdgeInsets.only(left: 5.w),
  //               child: ClipRRect(
  //                 borderRadius: BorderRadius.circular(4.w),
  //                 child: Image.asset('assets/pro.png', 
  //                   width: 100.w,
  //                   height: 100.w,
  //                 ),
  //               ),
  //             ),
  //             Container(
  //               margin: EdgeInsets.only(left: 5.w),
  //               child: ClipRRect(
  //                 borderRadius: BorderRadius.circular(4.w),
  //                 child: Image.asset('assets/pro.png', 
  //                   width: 100.w,
  //                   height: 100.w,
  //                 ),
  //               ),
  //             ),
  //             Container(
  //               margin: EdgeInsets.only(left: 5.w),
  //               child: ClipRRect(
  //                 borderRadius: BorderRadius.circular(4.w),
  //                 child: Image.asset('assets/pro.png', 
  //                   width: 100.w,
  //                   height: 100.w,
  //                 ),
  //               ),
  //             ),
  //             Container(
  //               margin: EdgeInsets.only(left: 5.w),
  //               child: ClipRRect(
  //                 borderRadius: BorderRadius.circular(4.w),
  //                 child: Image.asset('assets/pro.png', 
  //                   width: 100.w,
  //                   height: 100.w,
  //                 ),
  //               ),
  //             ),
  //             Container(
  //               margin: EdgeInsets.only(left: 5.w),
  //               child: ClipRRect(
  //                 borderRadius: BorderRadius.circular(4.w),
  //                 child: Image.asset('assets/pro.png', 
  //                   width: 100.w,
  //                   height: 100.w,
  //                 ),
  //               ),
  //             )
  //           ],
  //         )
  //       ],
  //     ),
  //   );
  // }

  Widget _buildPageOne() {
    if(loadSuccess == true) {
      return SingleChildScrollView(
        padding: EdgeInsets.fromLTRB(25.w, 0.w, 25.w, 60.w),
        child: Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                width: 700.w,
                margin: EdgeInsets.only(top: 24.w),
                padding: EdgeInsets.only(bottom: 34.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20.w),
                ),
                child: Column(
                  children: <Widget>[
                    _buildCardHead('基本信息'),
                    Container(
                      padding: EdgeInsets.fromLTRB(24.w, 20.w, 24.w, 0),
                      child: Column(
                        children: <Widget>[
                          _buildRowText('物资编号', detail['code']),
                          _buildRowText('物资名称', detail['goods_name']),
                          _buildRowText('规格/型号', detail['goods_model']),
                          _buildRowText('物资分类', detail['type_name']),
                          _buildRowText('入库类型', detail['enter_type'] == 1 ? '常规入库' : '暂存入库'),
                          _buildRowText('计量单位', detail['goods_unit']),
                          _buildRowText('产品估价', detail['goods_price']),
                          _buildRowText('有效期', detail['valid_time']),
                          _buildRowText('来源单位', detail['goods_source']),
                          _buildRowText('所属仓库', detail['store_name']),
                          _buildRowText('所属库区', detail['store_library_name']),
                          _buildRowText('所属库位', detail['store_detail_code']),
                          _buildRowText('入库人', detail['enter_id']),
                          _buildRowText('入库时间', detail['create_time']),
                          _buildRowText('RFID编码', detail['rfid_code']),
                          _buildRowText('入库工单', detail['entry_order_no']),
                          _buildRowPicture(),
                          _buildRowText('备注', detail['remark']),
                        ],
                      ),
                    )
                  ],
                ),
              ),
              Container(
                width: 700.w,
                margin: EdgeInsets.only(top: 24.w),
                padding: EdgeInsets.only(bottom: 34.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20.w),
                ),
                child: Column(
                  children: <Widget>[
                    _buildCardHead('数量统计'),
                    Container(
                      padding: EdgeInsets.fromLTRB(24.w, 20.w, 24.w, 0),
                      child: Column(
                        children: <Widget>[
                          _buildRowText('入库数量', detail['enter_qty']),
                          _buildRowText('在库数量', detail['real_qty']),
                          _buildRowText('出库数量', detail['out_qty']),
                          _buildRowText('拍卖数量', detail['auction_qty']),
                          _buildRowText('销毁数量', detail['destroy_qty']),
                          _buildRowText('自然损耗数量', detail['real_loss_qty']),
                          _buildRowText('取样数量', detail['sample_qty']),
                          _buildRowText('取样损耗', detail['sample_loss_qty']),
                          _buildRowText('捐赠数量', detail['donation_qty']),
                          _buildRowText('退还数量', detail['return_qty']),
                        ],
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        )
      );
    }else {
      return Container();
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return DefaultTabController(
      length: 3,
      initialIndex: 0,
      child: Scaffold(
        appBar: TopBar(
          title: '基本信息',
          backgroundColor: Utils.hexToColor('#297CFF'),
        ),
        body: _buildPageOne()
      )
    );
  }
}